package com.lkm.app_ui.m.Imple.fun.upapp;

import com.google.gson.annotations.SerializedName;
import com.lkm.app.event.OnNetworkStateEvent;
import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.help.AppHelp;
import com.lkm.app_ui.log.AppLogger;
import com.lkm.app_ui.netapi.AppApi;
import com.lkm.comlib.fun_m.NetworkStateModule;
import com.lkm.comlib.task2.DefaultTaskExecutor;
import com.lkm.frame.entity.ResponEntityAbs;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.javatool.StringHelp;
import com.lkm.javatool.VersionNumHelp;
import com.lkm.market2_0.entity.ResponEntity;

class CheckUpdateTask extends TaskAdapter<Void, Void, ResponEntityAbs<CheckUpdateBo>> {


   public CheckUpdateTask() {
       super(new DefaultTaskExecutor(AppConst.getTaskExecutor(), true), CheckUpdateTask.class.getName(), AppConst.getTaskManager2());
   }

   @Override
   public void doExecuting(Void param) {
       super.doExecuting(param);

       mTaskExecutor.executorDoAsyncExecuting(new Runnable() {
           @Override
           public void run() {
               ayncdoExecuting();
           }
       });

   }

   private void ayncdoExecuting() {

       String json = AppApi.versions(this);

       ResponEntity<VersionTo> responEntity = ResponEntity.fromJson(json, VersionTo.class, "");



       if (responEntity.isSuccess()) {
//            VersionTo versionTo = new VersionTo();// responEntity.getData().get(0);
//            versionTo.versions="1.1.1";

//            versionTo.downurl="http://zhongzi.web.ic1986.com/webapp/file/app-test-ui-dev-debug.txt";


           VersionTo versionTo = responEntity.getData();
//            versionTo.versions = "2.2";

           String localVersion = AppHelp.getLocalVersionName();
           boolean isUp = VersionNumHelp.compareVersion(localVersion, versionTo.versions) == -1;

           AppLogger.onCheckNewApk(isUp);


           CheckUpdateBo checkUpdateBo = new CheckUpdateBo(isUp, versionTo.isup == 1, "", versionTo.versions, localVersion);
           checkUpdateBo.isAuto = !"0".equals(versionTo.isAuto);
           checkUpdateBo.downUrl = versionTo.downurl;
           checkUpdateBo.tips = versionTo.tips;
           if (StringHelp.isEmpty(checkUpdateBo.tips)) {
               checkUpdateBo.tips = "有新版本，请更新";
           }

           complete(new ResponEntity<CheckUpdateBo>().success(checkUpdateBo, responEntity.getMsg()));

       } else {
           
           if(json == null){
               requestRe();
           }else {
               complete(new ResponEntity<CheckUpdateBo>().fail(null, responEntity.getMsg()));
           }

       }


   }













    
    int isReForNetOK = 0;
    private void requestRe() {
        if (!getIsContinue()) return;

        if(isReForNetOK>=5){
            complete(new ResponEntity<CheckUpdateBo>().fail(null, ""));
            return;
        }
        
        if (NetworkStateModule.getInstance().getIsNetworkOK()) {
            isReForNetOK ++;
            doExecuting(null);
        } else {
            
            if (observerAutoNext != null)
                LinkBus.unregisterReceiver(observerAutoNext);
            LinkBus.registerReceiver(getobserverAutoNext());
        }
    }

    ObserverAutoNext observerAutoNext;

    private ObserverAutoNext getobserverAutoNext() {
        if (observerAutoNext == null) {
            observerAutoNext = new ObserverAutoNext<OnNetworkStateEvent>() {
                @Override
                public void onReceiver(OnNetworkStateEvent data) {
                    if (!getIsContinue()) {
                        LinkBus.unregisterReceiver(this);
                        return;
                    }
                    if (data.extIsNetworkOK) {
                        LinkBus.unregisterReceiver(this);
                        doExecuting(null);
                    }
                }

                @Override
                public Class getBusEventClass() {
                    return OnNetworkStateEvent.class;
                }
            };
        }
        return observerAutoNext;
    }

    @Override
    public boolean onInterceptEnd(boolean isCancel, ResponEntityAbs<CheckUpdateBo> result) {
        if (observerAutoNext != null)
            LinkBus.unregisterReceiver(observerAutoNext);
        return super.onInterceptEnd(isCancel, result);
    }

    

   private static class VersionTo {

       @SerializedName("versions")
       public String versions;
       @SerializedName("isup")
       public int isup;

       
       @SerializedName("isAuto")
       public String isAuto = "";

       @SerializedName("download")
       public String downurl;

       @SerializedName("tips")
       public String tips;

   }
}
